#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project    :icac2 
@File       :serializers.py
@Author     :wangfan13
@Date       :2021/5/25 15:33 
"""
import re

from rest_framework import serializers
from apps.users.models import User


# 创建序列化器类，回头会在视图中被调用

class UserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        exclude = ['password']


class UserCreateSerializer(serializers.ModelSerializer):
    """创建用户序列化"""
    username = serializers.CharField(required=True)
    phone = serializers.CharField(max_length=11, required=False, allow_blank=True)

    class Meta:
        model = User
        fields = ['id', 'username', "password", 'phone', 'email', 'avatar']

    def validate_username(self, username):
        if User.objects.filter(username=username):
            raise serializers.ValidationError(username + "账号已存在")
        return username


class UserUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        # fields = ['id', 'username', 'phone', 'email', 'avatar', 'is_active']
        fields = ['id', 'username', 'phone', 'email', 'avatar', 'is_active', 'remark']